package com.iseastar.guojiang.team;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;

import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.CourierTeamMemberApplyListBean;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.team.adapter.CourierTeamMemberApplyListAdapter;
import com.shouhuobao.station.R;

import java.util.ArrayList;

import droid.frame.view.xlist.SListView;

/**
 * 队员申请列表
 * Created by lidandan on 2018/6/15.
 */
public class CourierTeamMemberApplyListActivity extends BaseActivity2 {

    private SListView mListiew;
    private CourierTeamMemberApplyListAdapter mAdapter = null;
    private int teamId;
    private ArrayList<CourierTeamMemberApplyListBean> items;
    private CourierTeamMemberApplyListBean item;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        teamId = getIntent().getIntExtra("teamId", 0);
        super.onCreate(savedInstanceState);
        showLoadingDialog(null);
        AppHttp.getInstance().teamApplyList(teamId);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.courier_team_member_apply_list_activity);
        super.findViewById();
        getAppTitle().setCommonTitle("申请列表");
        mListiew = findViewById(R.id.listview);
        mAdapter = new CourierTeamMemberApplyListAdapter(null, getContext());
        mListiew.setAdapter(mAdapter);
        mListiew.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                item = (CourierTeamMemberApplyListBean) parent.getItemAtPosition(position);
                if (item != null) {
                    if (item.getStatus() == 1) {
                        Intent intent = new Intent(CourierTeamMemberApplyListActivity.this, CourierTeamMemberApplyListDetailActivity.class);
                        intent.putExtra("item", item);
                        startActivity(intent);
                    }

                }
            }
        });
    }

    @Override
    public boolean handleMessage(final Message msg) {
        switch (msg.what) {
            //战队申请列表
            case MsgID.courier_team_apply_list:
                cancelLoadingDialog();
                final ReqResult<CourierTeamMemberApplyListBean> result = JSON.parseList(msg.obj, CourierTeamMemberApplyListBean.class);
                if (checkLoginStatus(result)) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            items = result.getResultList();
                            if (mAdapter == null) {
                                mAdapter = new CourierTeamMemberApplyListAdapter(items, getContext());
                                mListiew.setAdapter(mAdapter);
                            } else {
                                mAdapter.setItems(items, true);
                            }
                        }
                    });
                } else {
                    showToast(result.getMessage());
                }
                return true;
            case MsgID.team_member_check:
                cancelLoadingDialog();
                final ReqResult result1 = JSON.parser(msg.obj);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (checkLoginStatus(result1)) {
                            int status = msg.arg1;
                            if (item != null) {
                                item.setStatus(status);
                                mAdapter.notifyDataSetChanged();
                            }

                        } else {
                            showToast(result1.getMessage());
                        }
                    }
                });
                return false;
            default:
                return super.handleMessage(msg);
        }

    }
}
